#include "enemy_6.h"

extern int G_W, G_H;
extern QPoint shuttle_pos;

ENEMY_6::ENEMY_6(SCENE *scene, QTimeLine *global_tl, CASH *cash):QGraphicsObject()
{

    go = new SOUND_POWER(cash,"Res/Sound/Enemy/Enemy_6/go.ogg",this);
    go->Play_Now();

    qsrand(QTime::currentTime().second());

    point=50;

    this->cash = cash;

    border = new QPainterPath();
    im = cash->cash_pixmap->value("Res/Ships/Enemy/Enemy_6/Enemy_6.png");

    this->setZValue(0.5);

    anim_enemy = new QGraphicsItemAnimation();

    enemy_time_line = new QTimeLine(5000);                                             // Скорость перемещения обьекта по траектории
    enemy_time_line->setEasingCurve( QEasingCurve::Type(1));

    enemy_time_line->setFrameRange(0, 1);

    anim_enemy->setTimeLine(enemy_time_line);
    anim_enemy->setItem(this);

    i_state_in_left= 0+qrand()%(1-0+1);

    if(i_state_in_left){
        QPoint first_step = QPoint( (300+qrand()%(600-300+1))*(-1), 0+qrand()%(G_H/2-0+1) );
        this->setPos(first_step);
        anim_enemy->setPosAt(0,first_step);
        anim_enemy->setRotationAt(0,0+qrand()%(360-0+1));
    }
    else{
        QPoint first_step = QPoint( G_W+300+qrand()%(G_W+600-G_W+300+1), 0+qrand()%(G_H/2-0+1) );
        this->setPos(first_step);
        anim_enemy->setPosAt(0,first_step);
        anim_enemy->setRotationAt(0,0+qrand()%(360-0+1));
    }

    if( i_state_in_left ){
        anim_enemy->setPosAt(0.1,QPoint(100,50));
        anim_enemy->setRotationAt(0.1,0);
    }
    else{
        anim_enemy->setPosAt(0.1,QPoint(G_W-300,50));
        anim_enemy->setRotationAt(0.1,0);
    }

    //__________________

    if(i_state_in_left){

        if(shuttle_pos.y()>this->pos().y() && shuttle_pos.x()>this->pos().x()){
            anim_enemy->setRotationAt(0.2,-45);
            angle_attacks=(-45);
        }

        if(shuttle_pos.x()<this->pos().x() && shuttle_pos.y()>this->pos().y()){
            anim_enemy->setRotationAt(0.2,0);
            angle_attacks=0;
        }

        if(shuttle_pos.x()>this->pos().x() && shuttle_pos.y()<this->pos().y()){
            anim_enemy->setRotationAt(0.2,-90);
            angle_attacks=(-90);
        }

    }
    else{

        if(shuttle_pos.x()<this->pos().x() && shuttle_pos.y()>this->pos().y()){
            anim_enemy->setRotationAt(0.2,45);
            angle_attacks=45;
        }

        if(shuttle_pos.y()<this->pos().y() && shuttle_pos.x()<this->pos().x()){
            anim_enemy->setRotationAt(0.2,90);
            angle_attacks=90;
        }

        if(shuttle_pos.x()>this->pos().x() && shuttle_pos.y()>this->pos().y()){
            anim_enemy->setRotationAt(0.2,0);
            angle_attacks=0;
        }

    }

    anim_enemy->setPosAt(1,QPoint( 500+qrand()%(G_W-500+1),-500 ));
    anim_enemy->setRotationAt(1, 0+qrand()%(360-0+1) );

    enemy_time_line->start();

    fp_left = QPoint(19,81);                                                           // Точка вылета левого патрона
    fp_right = QPoint(125,81);                                                         // Точка вылета правого патрона

    collisia_control = new COLLISIA_CONTROL(scene,((QGraphicsObject*)this),this,global_tl,cash,10);

    connect(collisia_control,SIGNAL(Collisia(QPointF,int)),this,SLOT(Damage_Detected(QPointF,int)));
    connect(enemy_time_line,SIGNAL(finished()),SLOT(deleteLater()));
    connect(enemy_time_line,SIGNAL(valueChanged(qreal)),SLOT(Waiting_To_Fire(qreal)));


    QTimer::singleShot(50, this, SLOT(slot_set_shape()));
}

QPainterPath ENEMY_6::shape() const
{

    return *border;

}

QRectF ENEMY_6::boundingRect() const
{

    return QRectF( QPointF(0,0),im->size() );


}

void ENEMY_6::paint(QPainter *painter, const QStyleOptionGraphicsItem *, QWidget *)
{

    painter->save();
    painter->drawPixmap( 0,0,*im );
    painter->restore();

}

ENEMY_6::~ENEMY_6()
{
}

int ENEMY_6::type() const
{

    return point;

}

void ENEMY_6::Waiting_To_Fire(qreal r)
{

    if(r>0.100000 && r<0.150000 ){
        emit Fire(true,angle_attacks,true,this->pos().toPoint() );
        emit Fire(false,angle_attacks,false,this->pos().toPoint() );
    }

}

void ENEMY_6::Damage_Detected(QPointF a, int d)
{

    a = QPointF();
    point-=d;

    if(point<=0)
        if(this->scene()){
            disconnect(enemy_time_line,SIGNAL(finished()),this,SLOT(deleteLater()));
            go->Stop();
            emit Kill_Me_Please((QGraphicsObject*)this);
        }

}

void ENEMY_6::slot_set_shape()
{
    this->border = cash->cash_poly->value("Res/Ships/Enemy/Enemy_6/Enemy_6.poly");
}









